package com.nanhai.buddhist.college.module.study.controller.admin.studyevaluationscoreitem;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.nanhai.buddhist.college.framework.common.util.json.JsonUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;

import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.nanhai.buddhist.college.module.study.controller.admin.studyevaluationscoreitem.vo.*;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studyevaluationscoreitem.StudyEvaluationScoreItemDO;
import com.nanhai.buddhist.college.module.study.convert.studyevaluationscoreitem.StudyEvaluationScoreItemConvert;
import com.nanhai.buddhist.college.module.study.service.studyevaluationscoreitem.StudyEvaluationScoreItemService;

@Api(tags = "管理后台 - 评分配置")
@RestController
@RequestMapping("/study/evaluation-score-item")
@Validated
public class StudyEvaluationScoreItemController {

    @Resource
    private StudyEvaluationScoreItemService evaluationScoreItemService;

    @PostMapping("/create")
    @ApiOperation("创建评分配置")
//    @PreAuthorize("@ss.hasPermission('study:evaluation-score-item:create')")
    public CommonResult<String> createEvaluationScoreItem(@Valid @RequestBody StudyEvaluationScoreItemCreateReqVO createReqVO) {
        return success(evaluationScoreItemService.createEvaluationScoreItem(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新评分配置")
//    @PreAuthorize("@ss.hasPermission('study:evaluation-score-item:update')")
    public CommonResult<Boolean> updateEvaluationScoreItem(@Valid @RequestBody StudyEvaluationScoreItemUpdateReqVO updateReqVO) {
        evaluationScoreItemService.updateEvaluationScoreItem(updateReqVO);
        return success(true);
    }


    @PutMapping("/edit")
    @ApiOperation("更新评分配置")
//    @PreAuthorize("@ss.hasPermission('study:evaluation-score-item:update')")
    public CommonResult<Boolean> editEvaluationScoreItem(@Valid @RequestBody Map<String, Object> data) {
        Object evaluationScoreItemsObj = data.get("evaluationScoreItems");
        String jsonString = JsonUtils.toJsonString(evaluationScoreItemsObj);
        List<StudyEvaluationScoreItemUpdateReqVO> evaluationScoreItems = JsonUtils.parseArray(jsonString,StudyEvaluationScoreItemUpdateReqVO.class);

        Object delevaluationScoreItemsObj = data.get("delevaluationScoreItems");
        String jsonString2 = JsonUtils.toJsonString(delevaluationScoreItemsObj);
        List<String> delevaluationScoreItems = JsonUtils.parseArray(jsonString2,String.class);

        evaluationScoreItemService.editEvaluationScoreItem(evaluationScoreItems,delevaluationScoreItems);
        return success(true);
    }



    @DeleteMapping("/delete")
    @ApiOperation("删除评分配置")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
//    @PreAuthorize("@ss.hasPermission('study:evaluation-score-item:delete')")
    public CommonResult<Boolean> deleteEvaluationScoreItem(@RequestParam("id") String id) {
        evaluationScoreItemService.deleteEvaluationScoreItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得评分配置")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
//    @PreAuthorize("@ss.hasPermission('study:evaluation-score-item:query')")
    public CommonResult<StudyEvaluationScoreItemRespVO> getEvaluationScoreItem(@RequestParam("id") String id) {
        StudyEvaluationScoreItemDO evaluationScoreItem = evaluationScoreItemService.getEvaluationScoreItem(id);
        return success(StudyEvaluationScoreItemConvert.INSTANCE.convert(evaluationScoreItem));
    }

    @GetMapping("/list-byids")
    @ApiOperation("获得评分配置列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
//    @PreAuthorize("@ss.hasPermission('study:evaluation-score-item:query')")
    public CommonResult<List<StudyEvaluationScoreItemRespVO>> getEvaluationScoreItemList(@RequestParam("ids") Collection<String> ids) {
        List<StudyEvaluationScoreItemDO> list = evaluationScoreItemService.getEvaluationScoreItemList(ids);
        return success(StudyEvaluationScoreItemConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得评分配置分页")
//    @PreAuthorize("@ss.hasPermission('study:evaluation-score-item:query')")
    public CommonResult<PageResult<StudyEvaluationScoreItemRespVO>> getEvaluationScoreItemPage(@Valid StudyEvaluationScoreItemPageReqVO pageVO) {
        PageResult<StudyEvaluationScoreItemDO> pageResult = evaluationScoreItemService.getEvaluationScoreItemPage(pageVO);
        return success(StudyEvaluationScoreItemConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出评分配置 Excel")
//    @PreAuthorize("@ss.hasPermission('study:evaluation-score-item:export')")
    @OperateLog(type = EXPORT)
    public void exportEvaluationScoreItemExcel(@Valid StudyEvaluationScoreItemExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<StudyEvaluationScoreItemDO> list = evaluationScoreItemService.getEvaluationScoreItemList(exportReqVO);
        // 导出 Excel
        List<StudyEvaluationScoreItemExcelVO> datas = StudyEvaluationScoreItemConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "评分配置.xls", "数据", StudyEvaluationScoreItemExcelVO.class, datas);
    }


    @GetMapping("/list")
    @ApiOperation("获得评分配置列表")
//    @PreAuthorize("@ss.hasPermission('study:evaluation-score-item:query')")
    public CommonResult<List<StudyEvaluationScoreItemRespVO>> searchEvaluationScoreItemList(@Valid StudyEvaluationScoreItemExportReqVO searchVo) {
        List<StudyEvaluationScoreItemDO> list = evaluationScoreItemService.getEvaluationScoreItemList(searchVo);
        return success(StudyEvaluationScoreItemConvert.INSTANCE.convertList(list));
    }

}
